<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Account\Back\EmployeeList;
use AuctionCore\Common\AjaxPage;
use AuctionCore\System\AuctionException;
use AuctionCore\System\SystemLog;

class ClcwMessageController extends BaseController
{
    private $clcwMessageLogic;

    private $empId;

    private $empName;

    public function __construct()
    {
        parent::__construct();
        $this->empId = $this->user_info['emp_id'];
        $this->empName = $this->user_info['real_name'];
        $this->clcwMessageLogic = D('ClcwMessage', 'Logic');
        $this->assign("emp_id", $this->empId);
    }

    public function index()
    {
        $this->display();
    }

    //消息列表
    public function messageList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['title'] = I('get.title', '', 'trim');
        //获取list
        $result = $this->clcwMessageLogic->getClcwMessageList($page, $size, $params);
        $emp = [];
        if ($result['emp_id']) {
            $empList = new EmployeeList();
            $emp = $empList->getEmpByIds($result['emp_id']);
        }
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign('emp', $emp);
        $this->display();
    }


    public function addMessage()
    {
        $this->display();
    }

    /**
     * 添加消息
     */
    public function addMessageData()
    {
        $data['title'] = I("post.title", '', 'trim');
        $receiveIds = I("post.receive_ids", '', 'trim');
        if (!empty($receiveIds)) {
            $data['receive'] = 2;
            $receiveArr = explode(',', $receiveIds);
            if (count($receiveArr) != count(array_unique($receiveArr))) {
                $this->helpJsonResult(400, "抱歉，存在重复的收信人");
            }
            foreach ($receiveArr as $v) {
                $res = D('ClcwUser', 'Logic')->getUserInfoByUid($v);
                if (empty($res)) {
                    $this->helpJsonResult(400, "抱歉，存在无效的收信人");
                }
            }
        } else {
            $data['receive'] = 1;
        }
        $data['receive_ids'] = $receiveIds;
        $data['sender'] = 2;
        $data['params'] = I("post.params", '', 'trim');
        $content = I('post.content');
        $data['content'] = $this->paseContent($content);
        $data['status'] = 2;
//        $data['emp_id'] = $this->empId;

        if (empty($data['title'])) {
            $this->helpJsonResult(400, "消息标题不能为空！");
        }
        if (!$content) {
            $this->helpJsonResult(400, "消息内容不能为空！");
        }
        try {
            $res = $this->clcwMessageLogic->addClcwMessage($data);
            if ($res) {
                SystemLog::add(['emp_id' => $this->empId, 'action' => '添加消息']);
                $this->helpJsonResult(200, '消息添加成功！');
            } else {
                $this->helpJsonResult(500, '添加失败！');
            }
        } catch (AuctionException $e) {
            echo $e->getMessage();
        }
    }


    //消息展示页面
    public function editMessage()
    {
        $id = I('get.id/d', 0);
        $message = $this->clcwMessageLogic->getClcwMessage($id);
        $message['receive_ids'] = D('ClcwMessageReceive', 'Logic')->getUserIdsByMsgId($message['id']);
        $message['content'] = str_replace('</p>', "\n", $message['content']);
        $message['content'] = str_replace('<br />', "\n", $message['content']);
        $message['content'] = strip_tags($message['content']);
        $this->assign("message", $message);
        $this->display();
    }

    //执行修改
    public function editMessageData()
    {
        $id = I('post.msg_id/d');
        $data['title'] = I('post.title', '', 'trim');
        $receiveIds = I("post.receive_ids", '', 'trim');
        if (!empty($receiveIds)) {
            $data['receive'] = 2;
            $receiveArr = explode(',', $receiveIds);
            if (count($receiveArr) != count(array_unique($receiveArr))) {
                $this->helpJsonResult(400, "抱歉，存在重复的收信人");
            }
            foreach ($receiveArr as $v) {
                $res = D('ClcwUser', 'Logic')->getUserInfoByUid($v);
                if (empty($res)) {
                    $this->helpJsonResult(400, "抱歉，存在无效的收信人");
                }
            }
        } else {
            $data['receive'] = 1;
        }
        $data['receive_ids'] = $receiveIds;
        $data['sender'] = 2;
        $data['params'] = I("post.params", '', 'trim');
        $content = I('post.content', '');
        if (!$content) {
            $this->helpJsonResult(400, "消息内容不能为空！");
        }
        $data['content'] = $this->paseContent($content);
        $data['status'] = 2;
        $res = $this->clcwMessageLogic->saveClcwMessage($id, $data);
        if ($res === false) {
            SystemLog::add(['emp_id' => $this->empId, 'action' => '修改消息']);
            $this->helpJsonResult(500, '修改失败');
        } else {
            $this->helpJsonResult(200, '修改成功');
        }
    }

    public function paseContent($content)
    {
        $oldContent = explode("\n", $content);
        $content = array_map("trim", $oldContent);
        foreach ($content as $key => $val) {
            if ($key == 0) {
                $newContent[] = '<p class="notice-content-text">' . $val . '</p>';
            } else if (empty($content[$key])) {
                $newContent[] = '<p style="padding-top:10px;">' . $content[$key] . '</p>';
            } else {
                $newContent[] = '<p>' . $val . '</p>';
            }
        }
        $content = implode($newContent);
        return $content;
    }

    public function changeStatus()
    {
        $id = I('get.id');
        $data = [
            'status'       => 1,
            'publish_time' => NOW_TIME,
            'update_time'  => NOW_TIME
        ];

        $res = $this->clcwMessageLogic->saveClcwMessage($id, $data);
        if ($res === false) {
            SystemLog::add(['emp_id' => $this->empId, 'action' => '发布消息']);
            $this->helpJsonResult(500, '修改失败');
        } else {
            $this->helpJsonResult(200, '修改成功');
        }
    }


}
